﻿using CPPEI.Net;
using CPPEI.ShareScreen.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ShareScreen.Server.Handler
{
    public class RDDisconnectHandler : BaseRequestHandler<ReqRDDisconnectData, RespRDDisconnectData>
    {
        protected override RespRDDisconnectData OnHandle(NetServerData serverData, NetClientProxy requestClient, NetPackage request, ReqRDDisconnectData requestData)
        {
            NetClientProxy targetClient = serverData.GetClient(requestData.TargetClientID);

            NetRoomData room = serverData.GetRoom(requestData.RoomID);
            serverData.RemoveRoom(room);

            SendToTarget(request, room, targetClient);

            RespRDDisconnectData responseData = new RespRDDisconnectData();
            responseData.RoomID = room.ID;
            responseData.TargetClientID = requestData.TargetClientID;
            responseData.IsTargetClient = false;

            return responseData;
        }

        private void SendToTarget(NetPackage request, NetRoomData room, NetClientProxy targetClient)
        {
            RespRDDisconnectData responseData = new RespRDDisconnectData();
            responseData.RoomID = room.ID;
            responseData.TargetClientID = (uint)targetClient.Id;
            responseData.IsTargetClient = true;

            NetPackage package = new NetPackage(request.Command, request.ReplyMode, 0, Serializer.ToBinary(responseData));

            targetClient.Send(package);
        }
    }
}
